﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ClassManager_AddBIGType : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {

            EntityOprHelper helper = new EntityOprHelper();
            DataTable _table = helper.GetDataTableBySql("select TYPEID,NAME FROM T_TYPE");

            pType.DataValueField = "TYPEID";
            pType.DataTextField = "NAME";

            pType.DataSource = _table.DefaultView;
            pType.DataBind();


            RECORDID.Text = "";
            String rID = Page.Request["ID"];
            if (rID != null && rID != "")
            {
               
                T_PRODUCT_BIGTYPE add = helper.GetEntityByWhere<T_PRODUCT_BIGTYPE>(" where ID=" + rID + "");
                if (add != null)
                {
                    RECORDID.Text = add.ID.ToString();
                    pName.Text = add.NAME;
                    pImage.Text = add.IMAGE;
                    pType.SelectedValue = add.TYPEID;

                }
            }

        }

    }
    protected void Submit_Click(object sender, EventArgs e)
    {

         EntityOprHelper helper = new EntityOprHelper();

        if (RECORDID.Text == "")
        {
            if (pName.Text == "")
            {
                return;
            }
            T_PRODUCT_BIGTYPE _tp = helper.GetEntityByWhere<T_PRODUCT_BIGTYPE>(" where NAME='" + pName.Text + "';");
            if (_tp != null)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('该类型已经存在!')", true);
                return;
            }


            T_PRODUCT_BIGTYPE type = new T_PRODUCT_BIGTYPE();
            type.PID  = System.Guid.NewGuid().ToString("N").ToUpper();
            type.NAME = pName.Text;
            type.IMAGE = pImage.Text;
            type.TYPEID = pType.SelectedValue;
            bool b = helper.AddEntity<T_PRODUCT_BIGTYPE>(type);
            if (b)
            {
                pName.Text = "";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('添加成功!')", true);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('添加失败!')", true);
            }
        }
        else 
        {
            T_PRODUCT_BIGTYPE address = new T_PRODUCT_BIGTYPE();
            address.NAME = pName.Text;
            address.IMAGE = pImage.Text;
            address.TYPEID = pType.SelectedValue;
            address.ID =Convert.ToInt32(RECORDID.Text);

            bool b1 = helper.EditEntity<T_PRODUCT_BIGTYPE>(address);
            if (b1)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('修改成功')", true);
                pName.Text = "";
                RECORDID.Text = "";
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('修改失败')", true);
            }

            return;
        
        }
    


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        pImage.Text = shareCode.upFileToServer();
    }
}
